// import { login, logout } from '@/api/login'



// getInfo
import { getInfo, login, logout } from '@/mock/index.js';
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg'


import { authlogin } from '@/api/systemSetting/systemSetting.js';

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      token: getToken(),
      name: '',
      avatar: '',
      roles: [],
      permissions: []
    }),
    actions: {
      // 登录
      login(userInfo) {
        const userName = userInfo.userName.trim()
        const password = userInfo.password
        const code = userInfo.code
        const uuid = userInfo.uuid
        return new Promise((resolve, reject) => {
          // 
          // login(userName, password, code, uuid).then(res => {
          //   setToken(res.data.token)
          //   this.token = res.data.token
          //   resolve()
          // }).catch(error => {
          //   reject(error)
          // })
          // const nonce = Date.now().toString();
          /* "username":"admin",
            "password":"123456789abc" */

          let data = {
            // "address": "CaGTvRyDdohCZp2teEVws9Mu1NqVUeAwSrrsZ8ZGWoiC",
            // "signature": "3kfqxJZ5K9A4zRyPMWFjPbv5qf73h9LKbLhxNrvfUvHQXGJtDA7Rb1Gdds6QhF1h6gAbhhAEDgKaVDHyFoKM3ghp",
            // "nonce": '1745554091391',
            // "code": ""
            username: userName,
            password: password
          }
          authlogin(data).then(res => {
            console.log(res, ' api loing');

            // setToken(res.data.token)
            // this.token = res.data.token

            setToken(res)
            this.token = res

            resolve()
          }).catch(error => {
            reject(error)
          })


        })
      },
      // 获取用户信息
      getInfo() {
        return new Promise((resolve, reject) => {
          console.log(getInfo(), '2131');

          getInfo().then(res => {
            console.log(res, 'res');


            const user = res.user
            const avatar = (user.avatar == "" || user.avatar == null) ? defAva : import.meta.env.VITE_APP_BASE_API + user.avatar;

            if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
              this.roles = res.roles
              this.permissions = res.permissions
            } else {
              this.roles = ['ROLE_DEFAULT']
            }
            this.name = user.userName
            this.avatar = avatar;
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          logout(this.token).then(() => {
            this.token = ''
            this.roles = []
            this.permissions = []
            removeToken()
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      }
    }
  })

export default useUserStore
